/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.parser;

import java.io.IOException;


import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.Tree;

/**
 * Class to load a RIIF file and parse it into a Tree using the ANTLR generated classes
 * @author asanchep
 *
 */
public class ASTRIIFLoader {

	/**
	 * Loads a RIIF file and convert it into a Tree
	 * @param filename the path to the RIIF file
	 * @return The generated tree
	 * @throws IOException When the file name could not be read
	 */
	public Tree loadRIIF(String filename) throws IOException{
		CharStream input;
		try {
			input = new ANTLRFileStream(filename);
			RIIFLexer lex = new RIIFLexer(input);
			CommonTokenStream tokens = new CommonTokenStream(lex);
			RIIFParser parser = new RIIFParser(tokens);
			RIIFParser.program_return r = parser.program();
			Tree t = (Tree)r.tree;
			return t;
		} catch (IOException e) {
			//e.printStackTrace();
			throw e;
		} catch (RecognitionException e) {
			e.printStackTrace();
		}
		return null;
	}
}
